<?php
namespace App\Http\Controllers;

use App\Models\Activity;
use App\Models\Project;
use App\Models\Maker;
use App\Models\MakerProject;
use App\Models\Type;
use App\Http\Libs\Helper_Huanxin;
use App\Models\Zone;
use App\Models\Ad;

use App\Http\Controllers\Controller;
use App\User;
use \DB;
use Auth;
use Validator;
use Illuminate\Support\Facades\Request;
use App\Http\Requests\AdRequest;

class AdController extends CommonController
{

    /**
     * 活动列表。
     *
     * @return Response
     */
    public function getIndex()
    {
        $ads = DB::table('ad')->join('type', "ad.type", "=", "type.code")->select('ad.*', 'type.name as type_name')->orderBy('ad.id', 'desc')->paginate(10);

        return view('ad.index')->with("ads", $ads)->with("actions", $this->actions);
    }

    /**
     * 执行一个搜索
     */
    public function search(Request $request)
    {
        $data = Request::all();
        $sql = Ad::with('types');

        if (isset($data['title']) && $data['title'] != '') {
            $sql->where('title', 'like', '%' . $data['title'] . '%');
        }

        if (isset($data['type']) && $data['type'] != '') {
            $sql->where('type', $data['type']);
        }

        $lists = $sql->paginate(10);

        return view('ad.search')->with("lists", $lists)->with("title", $data['title'])
            ->with("type", $data['type'])->with("actions", $this->actions);
    }

    /**
     * 活动添加页面
     *
     * @return Ambigous <\Illuminate\View\$this, \Illuminate\View\\Illuminate\View\View>
     */
    public function getCreate()
    {
        $ad_types = DB::table('type as t1')->join('type as t2', "t1.upid", "=", "t2.id")->where('t2.code', 'ad_type')->select('t1.*')->get();

        return view('ad/create')->with('types', $ad_types);
    }

    /**
     * 添加广告
     *
     * @return string
     */
    public function postInsert(AdRequest $request)
    {
        $data = Request::all();
    	$data['start_time'] = strtotime($data['start_time']);
    	$data['expired_time'] = strtotime($data['expired_time']);
        $data['created_at'] = time();
        $data['updated_at'] = time();
        $data['status'] = 1;
        $id = DB::table("ad")->insertGetId($data);
        if ($id) {
            return AjaxCallbackMessage('广告添加成功', true, url('ad/index'));
        } else {
            return AjaxCallbackMessage('广告添加失败', false);
        }
    }

    /**
     * 编辑活动
     *
     * @return Ambigous <\Illuminate\View\$this, \Illuminate\View\\Illuminate\View\View>
     */
    public function getEdit()
    {
        if (!Request::has('aid')) {
            return $this->tip('参数异常', url('ad/index'), false);
        }
        $aid = Request::input('aid');
        $types = DB::table('type as t1')->join('type as t2', "t1.upid", "=", "t2.id")->where('t2.code', 'ad_type')->select('t1.*')->get();
        $ad = DB::table('ad')->where('id', $aid)->first();

        return view('ad.edit')->with("ad", $ad)->with('types', $types);
    }

    /**
     *
     */
    public function getImage(Request $request)
    {
        $data = $request::input();
        $ad = DB::table('ad')->where('id', $data['aid'])->first();

        return view('ad.image')->with("ad", $ad);
    }

    /**
     * 编辑活动
     *
     * @return Ambigous <\Illuminate\View\$this, \Illuminate\View\\Illuminate\View\View>
     */
    public function getAddetail()
    {
        if (!Request::has('aid')) {
            return $this->tip('参数异常', url('ad/index'), false);
        }
        $aid = Request::input('aid');
        $types = DB::table('type as t1')->join('type as t2', "t1.upid", "=", "t2.id")->where('t2.code', 'ad_type')->select('t1.*')->get();
        $ad = DB::table('ad')->where('id', $aid)->first();

        return view('ad.detail')->with("ad", $ad)->with('types', $types);
    }

    /**
     * 更新角色信息
     *
     * @return string
     */
    public function postUpdate(AdRequest $request)
    {
        $data = Request::all();
        $aid = $data['id'];
    	$data['start_time'] = strtotime($data['start_time']);
    	$data['expired_time'] = strtotime($data['expired_time']);
        $data['updated_at'] = time();
        unset($data['id']);
        DB::table("ad")->where('id', $aid)->update($data);

        return AjaxCallbackMessage('广告编辑成功', true, url('ad/index'));
    }

    /**
     * 删除活动
     *
     * @return string
     */
    public function postDelete()
    {
        if (!Request::has('aid')) {
            return AjaxCallbackMessage('数据异常', false);
        }
        $aid = Request::input("aid");
        DB::table('ad')->where('id', '=', $aid)->delete();

        return AjaxCallbackMessage('广告删除成功', true, url('ad/index'));
    }
}